<?php
// +----------------------------------------------------------------------
// 管理员角色组
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Validate;
class AdminGroup extends Base
{
	//属性列表
    public function index()
    {
    	$where = array();
    	$where['is_del'] = 0;
    	$name = $this->request->param('name');
    	if ($name){
    		 $where['group_name'] = array('LIKE', '%'.$name.'%');
    	}
    	$list = model('AdminGroup')->where($where)->order('group_sort asc')->paginate(15);
    	$page = $list->render();
    	$this->assign('list', $list);
    	$this->assign('page', $page);
    	return $this->fetch();
    }
    //添加属性
    public function groupForm()
    {
    	$group_id = $this->request->param('group_id');
    	$group = model('AdminGroup')->get($group_id);
    	$this->assign('group', $group);
    	return $this->fetch();
    }
    //删除属性
    public function delGroup()
    {
    	$data['group_id'] = $this->request->param('group_id');
    	$data['is_del'] = 1;
    	$attr_status = model('AdminGroup')->update($data);
    	if ($attr_status){
            $attr_status->menus()->detach();
    		$this->create_adminlog('删除属性ID：'.$data['group_id'].'');
    		echo json_encode(['status'=>1,'msg'=>'删除成功']);exit();
    	}else{
    		echo json_encode(['status'=>0,'msg'=>'删除失败']);exit();
    	}
    }
    //保存属性
    public function saveGroup()
    {
    	$rule = [
	    'group_name'  => 'require|max:255',
	    'group_sort'  => 'number|between:0,127',
		];

		$msg = [
		    'group_name.require' => '角色组名称不能为空',
		    'group_name.max' => '角色组名称长度不能超过255个字符',
		    'group_sort' => '角色组排序请输入0-127之间',
		];

    	$data = $this->request->param();
    	$validate = new Validate($rule, $msg);
    	if (!$validate->check($data)){
    		echo json_encode(['status'=>0,'msg'=>$validate->getError()]);exit();
    	}

        $group_id=$data['group_id'];
    	if ($group_id){
    		$attr_status = model('AdminGroup')->where('group_id',$group_id)->update($data);
    	}else{
    		$attr_status = model('AdminGroup')->insertGetId($data);
    	}
    	if ($attr_status){
    		$this->create_adminlog('保存 '.$data['group_name'].' >'.$data['group_name'].' 角色组');
    		echo json_encode(['status'=>1,'msg'=>'保存成功']);exit();
    	}else{
    		echo json_encode(['status'=>0,'msg'=>'保存失败']);exit();
    	}
    }

    /**
     * 权限列表
     * @return mixed
     */
    public function powerList()
    {
        $group_id = $this->request->param('group_id');
        $power=model('AdminGroup')->get($group_id);
        //管理权限
        $check_power=[];
        if(!empty($power)){
            $check_powers=$power->menus;
            foreach($check_powers as $g){
                // 获取中间表数据
                $check_power=$g->pivot->where('w_group_id',$group_id)->column('w_menu_id');
            }
        }
        $this->assign('check_power', $check_power);

        //所有权限菜单
        $menu = model('AdminMenu')->all();
        $menu=model('AdminGroup')->getSubTreeb($menu);

        $this->assign('menu', $menu);

        $this->assign('group_id', $group_id);
        return $this->fetch();
    }
    /**
     * 权限保存
     * @return mixed
     */
    public function powerSave()
    {
        $post= $this->request->param();
        $groups=model('AdminGroup');
        $group_id=$post['group_id'];
        $rule = [
            'menu_id'  => 'require'
        ];
        $msg = [
            'menu_id' => '请选择权限',
        ];
        $validate = new Validate($rule, $msg);
        if (!$validate->check($post)){
            echo json_encode(['status'=>0,'msg'=>$validate->getError()]);exit();
        }

        $group=$groups->get($group_id);
        $group->menus()->detach();
        $attr_status = $group->menus()->attach($post['menu_id']);
        if ($attr_status){
            $this->create_adminlog('设置权限');
            echo json_encode(['status'=>1,'msg'=>'保存成功']);exit();
        }else{
            echo json_encode(['status'=>0,'msg'=>'保存失败']);exit();
        }
    }


}
